/*
543. 二叉树的直径
https://leetcode.cn/problems/diameter-of-binary-tree/description/
中等 2024/11/15
*/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
int ans=0;
int test(TreeNode* root)
{
    if(root==nullptr) return 0;
    int l=test(root->left);
    int r=test(root->right);
    ans=max(ans,l+r);
    int val=max(l,r)+1;
    return val;
}
    int diameterOfBinaryTree(TreeNode* root) {
       test(root);
        return ans;
    }
};